package cn.lzj.system.service.impl;

import cn.lzj.system.domain.Role;
import cn.lzj.system.mapper.RoleMapper;
import cn.lzj.system.service.IRoleService;
import cn.lzj.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    @Transactional
    public void add(Role role) {
        super.add(role);
        addDate(role);
    }
        private void addDate(Role role){
            List<Long> menus = role.getMenus();
            if(menus.size()>0){
                roleMapper.addRoleMenu(role.getId(),menus);
            }
            List<Long> permissions = role.getPermissions();
            if(permissions.size()>0){
                roleMapper.addRolePermissions(role.getId(),permissions);
            }
    }
    @Override
    @Transactional
    public void update(Role role) {
        roleMapper.deleteRoleMenuByRoleId(role.getId());
        roleMapper.deleteRolePermissionByRoleId(role.getId());
        addDate(role);
        super.update(role);
    }
    @Override
    @Transactional
    public void remove(Long id) {
        roleMapper.deleteRoleMenuByRoleId(id);
        roleMapper.deleteRolePermissionByRoleId(id);
        roleMapper.deleteRoleEmployeeByRoleId(id);
        super.remove(id);
    }
    @Override
    public void removeList(List<Long> ids) {
        super.removeList(ids);
        for(Long id : ids){
            remove(id);
        }
    }
}


